{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "tutor_cell",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Welcome to your AI Tutor! I'm here to help you learn step by step.\n",
      "I remember everything we discuss, so we can build upon previous topics.\n",
      "Type 'quit' to exit anytime.\n",
      "\n",
      "\n",
      "Thinking about: 'AI'\n",
      "\n"
     ]
    },
    {
     "data": {
      "text/markdown": [
       "It looks like you're interested in the topic of AI! Building on what we discussed previously, artificial intelligence (AI) encompasses a variety of technologies and concepts. We might have talked about its applications, like machine learning, natural language processing, or robotics.\n",
       "\n",
       "### Let's Explore AI Further\n",
       "Here are some key concepts we can dive into:\n",
       "\n",
       "1. **Machine Learning**: This is a subset of AI focused on algorithms that allow computers to learn from data and improve from experience without being explicitly programmed.\n",
       "\n",
       "2. **Natural Language Processing (NLP)**: This area enables computers to understand, interpret, and generate human language. You've probably seen this in applications like chatbots or translation services.\n",
       "\n",
       "3. **Robotics**: AI plays a significant role in how robots make decisions and navigate their environments.\n",
       "\n",
       "#### Questions to Consider:\n",
       "- Are you interested in a specific area of AI, like machine learning or NLP?\n",
       "- Do you want to learn about how AI is applied in real-world scenarios like healthcare or finance?\n",
       "\n",
       "Let me know which direction you’d like to take, or if there’s a specific question on AI you have in mind!"
      ],
      "text/plain": [
       "<IPython.core.display.Markdown object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "Thanks for learning with me! Goodbye!\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "import json\n",
    "from dotenv import load_dotenv\n",
    "from IPython.display import Markdown, display, update_display\n",
    "from openai import OpenAI\n",
    "\n",
    "load_dotenv(override=True)\n",
    "openai = OpenAI()\n",
    "\n",
    "tutor_system_prompt = \"\"\"\n",
    "You are an expert AI tutor engaged in an ongoing conversation with a student. This is a continuous learning session where you must remember and reference ALL previous topics discussed.\n",
    "\n",
    "IMPORTANT: Always connect new answers to previous conversation topics. Reference what you've taught before and build upon prior knowledge. If the student asks about something new, relate it to concepts already covered when possible.\n",
    "\n",
    "Be patient, encouraging, and adapt your teaching style to the student's needs.\n",
    "Break down complex concepts into simple steps, provide clear examples, and ask questions to check understanding.\n",
    "Use analogies, encourage critical thinking, and celebrate small victories.\n",
    "Respond in well-formatted markdown with clear sections, code examples when relevant, and interactive elements.\n",
    "\n",
    "When appropriate, say things like \"Building on what we discussed earlier about [topic]...\" or \"Remember when we talked about [concept]? Let's apply that here...\"\n",
    "Always end with a question or prompt to continue the learning conversation.\n",
    "\"\"\"\n",
    "\n",
    "conversation_history = []\n",
    "question_count = 0\n",
    "\n",
    "print(\"Welcome to your AI Tutor! I'm here to help you learn step by step.\")\n",
    "print(\"I remember everything we discuss, so we can build upon previous topics.\")\n",
    "print(\"Type 'quit' to exit anytime.\\n\")\n",
    "\n",
    "while True:\n",
    "    if question_count == 0:\n",
    "        user_input = input(\"What would you like to learn about today? \")\n",
    "    else:\n",
    "        user_input = input(f\"\\nQuestion {question_count + 1}: What's your next question? \")\n",
    "    \n",
    "    if user_input.lower() in ['quit', 'exit', 'bye']:\n",
    "        print(\"\\nThanks for learning with me! Goodbye!\")\n",
    "        break\n",
    "    \n",
    "    if not user_input.strip():\n",
    "        print(\"Please ask me a question!\")\n",
    "        continue\n",
    "    \n",
    "    question_count += 1\n",
    "    conversation_history.append({\"role\": \"user\", \"content\": user_input})\n",
    "    \n",
    "    print(f\"\\nThinking about: '{user_input}'\\n\")\n",
    "    \n",
    "    stream = openai.chat.completions.create(\n",
    "        model=\"gpt-4o-mini\",\n",
    "        messages=[\n",
    "            {\"role\": \"system\", \"content\": tutor_system_prompt}\n",
    "        ] + conversation_history,\n",
    "        stream=True\n",
    "    )\n",
    "    \n",
    "    response = \"\"\n",
    "    display_handle = display(Markdown(\"\"), display_id=True)\n",
    "    for chunk in stream:\n",
    "        response += chunk.choices[0].delta.content or ''\n",
    "        update_display(Markdown(response), display_id=display_handle.display_id)\n",
    "    \n",
    "    conversation_history.append({\"role\": \"assistant\", \"content\": response})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": ".venv",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
